package clientapp;

/*
 * Game.java
 *
 * Created on 2009-10-8, 23:25:27
 */
/**
 *
 * @author automne
 */
import controller.WordlistControl;
import controller.ScoreControl;
import ejb.GameUser;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class Game extends javax.swing.JFrame {

    private String letters;
    private String tempLetters;
    private DefaultListModel lModel = new DefaultListModel();
    private int singleScore;
    private int totalScore;
    private int timeLimit;
    private GameUser player;
    
    /** Creates new form Game */
    public Game() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.listWords.setModel(lModel);
        newGame(true);
    }

     public Game(GameUser player) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.listWords.setModel(lModel);
        this.player = player;
        newGame(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textWord = new javax.swing.JTextField();
        btnEnter = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lblPlayer = new javax.swing.JLabel();
        textPlayer = new javax.swing.JTextField();
        lblScore = new javax.swing.JLabel();
        textScore = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listWords = new javax.swing.JList();
        lblTitle = new javax.swing.JLabel();
        lblWords = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblAphb4 = new javax.swing.JLabel();
        lblAphb6 = new javax.swing.JLabel();
        lblAphb7 = new javax.swing.JLabel();
        lblAphb5 = new javax.swing.JLabel();
        lblAphb3 = new javax.swing.JLabel();
        lblAphb2 = new javax.swing.JLabel();
        lblAphb1 = new javax.swing.JLabel();
        lblAphb9 = new javax.swing.JLabel();
        lblAphb8 = new javax.swing.JLabel();
        lblAphb10 = new javax.swing.JLabel();
        lblTmCtDown = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        BackItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Welcome to Spelling Bees!");
        setAlwaysOnTop(true);

        textWord.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textWordActionPerformed(evt);
            }
        });
        textWord.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textWordKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textWordKeyTyped(evt);
            }
        });

        btnEnter.setText("ENTER");
        btnEnter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnterActionPerformed(evt);
            }
        });

        lblPlayer.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblPlayer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPlayer.setText("PLAYER");

        textPlayer.setEditable(false);
        textPlayer.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textPlayer.setText("Weiqi");

        lblScore.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblScore.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblScore.setText("SCORE");

        textScore.setEditable(false);
        textScore.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textScore.setText("0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(lblScore))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(textPlayer, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(textScore)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(lblPlayer)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblPlayer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textPlayer, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblScore)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textScore, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addContainerGap())
        );

        listWords.setAlignmentX(0.0F);
        listWords.setAlignmentY(0.0F);
        jScrollPane1.setViewportView(listWords);

        lblTitle.setFont(new java.awt.Font("Tempus Sans ITC", 3, 18));
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTitle.setText("Spelling Bees");

        lblWords.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblWords.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblWords.setText("Words");

        jPanel2.setAlignmentX(0.0F);
        jPanel2.setAlignmentY(0.0F);
        jPanel2.setAutoscrolls(true);

        lblAphb4.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb4.setText("A");
        lblAphb4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb4MouseClicked(evt);
            }
        });

        lblAphb6.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb6.setText("A");
        lblAphb6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb6MouseClicked(evt);
            }
        });

        lblAphb7.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb7.setText("A");
        lblAphb7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb7MouseClicked(evt);
            }
        });

        lblAphb5.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb5.setText("A");
        lblAphb5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb5MouseClicked(evt);
            }
        });

        lblAphb3.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb3.setText("A");
        lblAphb3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb3MouseClicked(evt);
            }
        });

        lblAphb2.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb2.setText("A");
        lblAphb2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb2MouseClicked(evt);
            }
        });

        lblAphb1.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb1.setText("A");
        lblAphb1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb1MouseClicked(evt);
            }
        });

        lblAphb9.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb9.setText("A");
        lblAphb9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb9MouseClicked(evt);
            }
        });

        lblAphb8.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb8.setText("A");
        lblAphb8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb8MouseClicked(evt);
            }
        });

        lblAphb10.setFont(new java.awt.Font("Times New Roman", 1, 36));
        lblAphb10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAphb10.setText("A");
        lblAphb10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAphb10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAphb10MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(lblAphb1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAphb2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(lblAphb3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAphb4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAphb5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAphb6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAphb7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAphb8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblAphb9)
                .addGap(10, 10, 10)
                .addComponent(lblAphb10)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(lblAphb10, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb9, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb8, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb7, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb6, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb5, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb4, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb3, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb2, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
            .addComponent(lblAphb1, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
        );

        lblTmCtDown.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblTmCtDown.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTmCtDown.setText("0:00");
        lblTmCtDown.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                lblTmCtDownPropertyChange(evt);
            }
        });

        fileMenu.setText("Game");

        newMenuItem.setText("New Game");
        newMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newMenuItem);

        BackItem.setText("Back to Main Menu");
        BackItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackItemActionPerformed(evt);
            }
        });
        fileMenu.add(BackItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);
        fileMenu.add(jSeparator1);

        menuBar.add(fileMenu);

        helpMenu.setText("Help");

        contentsMenuItem.setText("Game Rule");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap(46, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(btnEnter, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(56, 56, 56))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(textWord, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(35, 35, 35))))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(62, 62, 62)
                            .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                            .addComponent(lblTmCtDown, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(56, 56, 56)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblWords, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblTitle)
                        .addComponent(lblTmCtDown, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblWords, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                                .addComponent(textWord, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnEnter, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(8, 8, 8)))))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnterActionPerformed
        // TODO add your handling code here:
        checkWord();
    }//GEN-LAST:event_btnEnterActionPerformed

    private void textWordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textWordKeyTyped
        // TODO add your handling code here:
        evt.setKeyChar(String.valueOf(evt.getKeyChar()).toUpperCase().charAt(0));
        String keyChar = String.valueOf(evt.getKeyChar());
        if (tempLetters.contains(keyChar) == false || evt.getKeyChar() == evt.VK_SPACE) {
            evt.consume();
        } else if (tempLetters.contains(keyChar) == true) {
            int tempNo = tempLetters.indexOf(keyChar) + 1;
            switch (tempNo) {
                case 1:
                    lblAphb1.setText(" ");
                    break;
                case 2:
                    lblAphb2.setText(" ");
                    break;
                case 3:
                    lblAphb3.setText(" ");
                    break;
                case 4:
                    lblAphb4.setText(" ");
                    break;
                case 5:
                    lblAphb5.setText(" ");
                    break;
                case 6:
                    lblAphb6.setText(" ");
                    break;
                case 7:
                    lblAphb7.setText(" ");
                    break;
                case 8:
                    lblAphb8.setText(" ");
                    break;
                case 9:
                    lblAphb9.setText(" ");
                    break;
                case 10:
                    lblAphb10.setText(" ");
                    break;
            }
            tempLetters = tempLetters.replaceFirst(keyChar, " ");
        }
    }//GEN-LAST:event_textWordKeyTyped

    private void textWordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textWordKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == evt.VK_BACK_SPACE && textWord.getText().length() != 0) {
            char delChar = textWord.getText().charAt(textWord.getText().length() - 1);
            int index = letters.lastIndexOf(delChar);
            while (tempLetters.charAt(index) == delChar) {
                index = letters.lastIndexOf(delChar, index - 1);
            }
            tempLetters = replaceStr(index, index + 1, tempLetters, String.valueOf(delChar));
            switch (index + 1) {
                case 1:
                    lblAphb1.setText(String.valueOf(delChar));
                    break;
                case 2:
                    lblAphb2.setText(String.valueOf(delChar));
                    break;
                case 3:
                    lblAphb3.setText(String.valueOf(delChar));
                    break;
                case 4:
                    lblAphb4.setText(String.valueOf(delChar));
                    break;
                case 5:
                    lblAphb5.setText(String.valueOf(delChar));
                    break;
                case 6:
                    lblAphb6.setText(String.valueOf(delChar));
                    break;
                case 7:
                    lblAphb7.setText(String.valueOf(delChar));
                    break;
                case 8:
                    lblAphb8.setText(String.valueOf(delChar));
                    break;
                case 9:
                    lblAphb9.setText(String.valueOf(delChar));
                    break;
                case 10:
                    lblAphb10.setText(String.valueOf(delChar));
                    break;
            }
        }
    }//GEN-LAST:event_textWordKeyPressed

    private void lblAphb1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb1MouseClicked
        // TODO add your handling code here:
        if (lblAphb1.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb1.getText());
            tempLetters = replaceStr(0, 1, tempLetters, " ");
            lblAphb1.setText(" ");
        }
    }//GEN-LAST:event_lblAphb1MouseClicked

    private void lblAphb2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb2MouseClicked
        // TODO add your handling code here:
        if (lblAphb2.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb2.getText());
            tempLetters = replaceStr(1, 2, tempLetters, " ");
            lblAphb2.setText(" ");
        }
    }//GEN-LAST:event_lblAphb2MouseClicked

    private void lblAphb3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb3MouseClicked
        // TODO add your handling code here:
        if (lblAphb3.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb3.getText());
            tempLetters = replaceStr(2, 3, tempLetters, " ");
            lblAphb3.setText(" ");
        }
    }//GEN-LAST:event_lblAphb3MouseClicked

    private void lblAphb4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb4MouseClicked
        // TODO add your handling code here:
        if (lblAphb4.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb4.getText());
            tempLetters = replaceStr(3, 4, tempLetters, " ");
            lblAphb4.setText(" ");
        }
    }//GEN-LAST:event_lblAphb4MouseClicked

    private void lblAphb6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb6MouseClicked
        // TODO add your handling code here:
        if (lblAphb6.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb6.getText());
            tempLetters = replaceStr(5, 6, tempLetters, " ");
            lblAphb6.setText(" ");
        }
    }//GEN-LAST:event_lblAphb6MouseClicked

    private void lblAphb7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb7MouseClicked
        // TODO add your handling code here:
        if (lblAphb7.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb7.getText());
            tempLetters = replaceStr(6, 7, tempLetters, " ");
            lblAphb7.setText(" ");
        }
    }//GEN-LAST:event_lblAphb7MouseClicked

    private void textWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textWordActionPerformed
        // TODO add your handling code here:
        checkWord();
    }//GEN-LAST:event_textWordActionPerformed

    private void lblAphb5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb5MouseClicked
        // TODO add your handling code here:
        if (lblAphb5.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb5.getText());
            tempLetters = replaceStr(4, 5, tempLetters, " ");
            lblAphb5.setText(" ");
        }
    }//GEN-LAST:event_lblAphb5MouseClicked

    private void lblAphb9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb9MouseClicked
        // TODO add your handling code here:
        if (lblAphb9.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb9.getText());
            tempLetters = replaceStr(8, 9, tempLetters, " ");
            lblAphb9.setText(" ");
        }
    }//GEN-LAST:event_lblAphb9MouseClicked

    private void lblAphb8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb8MouseClicked
        // TODO add your handling code here:
        if (lblAphb8.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb8.getText());
            tempLetters = replaceStr(7, 8, tempLetters, " ");
            lblAphb8.setText(" ");
        }
    }//GEN-LAST:event_lblAphb8MouseClicked

    private void lblAphb10MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAphb10MouseClicked
        // TODO add your handling code here:
        if (lblAphb10.getText().equals(" ") != true) {
            textWord.setText(textWord.getText() + lblAphb10.getText());
            tempLetters = replaceStr(9, 10, tempLetters, " ");
            lblAphb10.setText(" ");
        }
    }//GEN-LAST:event_lblAphb10MouseClicked

    private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
        // TODO add your handling code here:
        newGame(false);
    }//GEN-LAST:event_newMenuItemActionPerformed

    private void lblTmCtDownPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_lblTmCtDownPropertyChange
        // TODO add your handling code here:
        if (lblTmCtDown.getText().equals("00:00") == true) {
            lblTmCtDown.updateUI();
            ScoreControl score = new ScoreControl("ejb.ScoreFacade");
            score.update(player.getId(), totalScore);
            int userChoice = JOptionPane.showOptionDialog(rootPane, "Game over! Your score is " + totalScore + ".Would you like to play again?", "Game Over", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
            if (userChoice == JOptionPane.YES_OPTION) {
                newGame(false);
            } else if (userChoice == JOptionPane.NO_OPTION) {
                new PlayerMain(player).setVisible(true);
                this.setVisible(false);
            }
        }
    }//GEN-LAST:event_lblTmCtDownPropertyChange

    private void BackItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackItemActionPerformed
        // TODO add your handling code here:
        new PlayerMain(player).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_BackItemActionPerformed

    private String replaceStr(int beginIndex, int endIndex, String str, String rplStr) {
        StringBuffer tempStr = new StringBuffer(str);
        tempStr = tempStr.replace(beginIndex, endIndex, rplStr);
        return tempStr.toString();
    }

    private void checkWord() {
        boolean wordIsExist = false;
        WordlistControl game = new WordlistControl("ejb.WordlistFacade");
        wordIsExist = game.find(textWord.getText());
        if (lModel.contains(textWord.getText()) == false && textWord.getText().length() != 0 && wordIsExist == true) {
            lModel.addElement(textWord.getText());
            textWord.setText("");
            tempLetters = letters;
            resetLbl();
            totalScore += singleScore;
            textScore.setText(String.valueOf(totalScore));
        }
        return;
    }

    private void resetLbl() {
        lblAphb1.setText(letters.substring(0, 1));
        lblAphb2.setText(letters.substring(1, 2));
        lblAphb3.setText(letters.substring(2, 3));
        lblAphb4.setText(letters.substring(3, 4));
        lblAphb5.setText(letters.substring(4, 5));
        lblAphb6.setText(letters.substring(5, 6));
        lblAphb7.setText(letters.substring(6, 7));
        lblAphb8.setText(letters.substring(7, 8));
        lblAphb9.setText(letters.substring(8, 9));
        lblAphb10.setText(letters.substring(9, 10));
        return;
    }

    private void newGame(boolean isInit) {
        lettGenerator(10);
        lModel.clear();
        singleScore = 5;
        totalScore = 0;
        timeLimit = 120;
        textPlayer.setText(player.getId());
        textScore.setText(String.valueOf(totalScore));
        textWord.setText("");
        textWord.requestFocusInWindow();
        resetLbl();
        TimeThread t = new TimeThread(lblTmCtDown, timeLimit);
        JOptionPane.showMessageDialog(rootPane, "Are you ready? Game will start immediately after you click OK.");
        t.start();
    }

    private void lettGenerator(int number) {
        this.letters = "";
        double[] range0 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        double[] range1 = {11.1607,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        double[] frequency = {11.1607,8.4966,7.5809,7.5448,7.1635,6.9509,6.6544,5.7351,5.4893,4.5388,3.6308,3.3844,3.1671,3.0129,3.0034,2.4705,2.0720,1.8121,1.7779,1.2899,1.1016,1.0074,0.2902,0.2722,0.1965,0.1962};
        for (int i = 1; i < 26; i++) {
            range0[i] = range1[i - 1];
            range1[i] = range1[i - 1] + frequency[i];
        }
        for (int i = 0 ; i < number ; i++) {
            double k = Math.random()*100;
            if (k >= range0[0] && k < range1[0]) {
                letters += "E";
            }
            if (k >= range0[1] && k < range1[1]) {
                letters += "A";
            }
            if (k >= range0[2] && k < range1[2]) {
                letters += "R";
            }
            if (k >= range0[3] && k < range1[3]) {
                letters += "I";
            }
            if (k >= range0[4] && k < range1[4]) {
                letters += "O";
            }
            if (k >= range0[5] && k < range1[5]) {
                letters += "T";
            }
            if (k >= range0[6] && k < range1[6]) {
                letters += "N";
            }
            if (k >= range0[7] && k < range1[7]) {
                letters += "S";
            }
            if (k >= range0[8] && k < range1[8]) {
                letters += "L";
            }
            if (k >= range0[9] && k < range1[9]) {
                letters += "C";
            }
            if (k >= range0[10] && k < range1[10]) {
                letters += "U";
            }
            if (k >= range0[11] && k < range1[11]) {
                letters += "D";
            }
            if (k >= range0[12] && k < range1[12]) {
                letters += "P";
            }
            if (k >= range0[13] && k < range1[13]) {
                letters += "M";
            }
            if (k >= range0[14] && k < range1[14]) {
                letters += "H";
            }
            if (k >= range0[15] && k < range1[15]) {
                letters += "G";
            }
            if (k >= range0[16] && k < range1[16]) {
                letters += "B";
            }
            if (k >= range0[17] && k < range1[17]) {
                letters += "F";
            }
            if (k >= range0[18] && k < range1[18]) {
                letters += "Y";
            }
            if (k >= range0[19] && k < range1[19]) {
                letters += "W";
            }
            if (k >= range0[20] && k < range1[20]) {
                letters += "K";
            }
            if (k >= range0[21] && k < range1[21]) {
                letters += "V";
            }
            if (k >= range0[22] && k < range1[22]) {
                letters += "X";
            }
            if (k >= range0[23] && k < range1[23]) {
                letters += "Z";
            }
            if (k >= range0[24] && k < range1[24]) {
                letters += "J";
            }
            if (k >= range0[25] && k <= range1[25]) {
                letters += "Q";
            }
        }
        tempLetters = letters;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem BackItem;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton btnEnter;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblAphb1;
    private javax.swing.JLabel lblAphb10;
    private javax.swing.JLabel lblAphb2;
    private javax.swing.JLabel lblAphb3;
    private javax.swing.JLabel lblAphb4;
    private javax.swing.JLabel lblAphb5;
    private javax.swing.JLabel lblAphb6;
    private javax.swing.JLabel lblAphb7;
    private javax.swing.JLabel lblAphb8;
    private javax.swing.JLabel lblAphb9;
    private javax.swing.JLabel lblPlayer;
    private javax.swing.JLabel lblScore;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblTmCtDown;
    private javax.swing.JLabel lblWords;
    private javax.swing.JList listWords;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JTextField textPlayer;
    private javax.swing.JTextField textScore;
    private javax.swing.JTextField textWord;
    // End of variables declaration//GEN-END:variables
}
